package com.artisan.medium.wx.utils;

import com.alibaba.fastjson.JSON;
import com.artisan.common.utils.wxpay.HttpClientUtil;
import com.artisan.common.utils.wxpay.PayConfig;
import com.artisan.medium.wx.pojo.AccessToken;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by Administrator on 2016/12/2 0002.
 */
public class AccessTokenUtils {

    private static final Logger logger = LogManager.getLogger(AccessTokenUtils.class);

    public static void main(String []args){
        System.out.println(PayConfig.WeixinGZHPayConfig.APP_ID);
    }

    public static AccessToken getAccessTokenUtil(String code) {
        Map<String, Object> resultData = new HashMap<String, Object>();
        HttpClientUtil util = HttpClientUtil.getInstance();
       /* resultData.put("appid", PayConfig.WeixinGZHPayConfig.APP_ID);
        resultData.put("secret", PayConfig.WeixinGZHPayConfig.APPSECRET);
        resultData.put("code", code);
        resultData.put("grant_type", PayConfig.WeixinGZHPayConfig.GRANT_TYPE);*/
        resultData.put("appid", "wx71b22bc16489b2ee");
        resultData.put("secret", "ecbeb836e79a445e1d0da5d3b97677fd");
        resultData.put("code", code);
        resultData.put("grant_type","authorization_code");
        String returnStr = null;
        try {
            returnStr = util.getResponseBodyAsString("https://api.weixin.qq.com/sns/oauth2/access_token", resultData);
        } catch (Exception e) {
            e.printStackTrace();
        }
        logger.info("returnStr:[" + returnStr + "]");
        AccessToken at = JSON.parseObject(returnStr, AccessToken.class);
        return at;
    }

    public static AccessToken
                         getToken(){
        Map<String, Object> resultData = new HashMap<String, Object>();
        HttpClientUtil util = HttpClientUtil.getInstance();
        /*resultData.put("appid", PayConfig.WeixinGZHPayConfig.APP_ID);
        resultData.put("secret", PayConfig.WeixinGZHPayConfig.APPSECRET);
        resultData.put("grant_type", "client_credential");*/

        resultData.put("appid", "wx71b22bc16489b2ee");
        resultData.put("secret", "ecbeb836e79a445e1d0da5d3b97677fd");
        resultData.put("grant_type","client_credential");
        String returnStr = null;
        try {
            returnStr = util.getResponseBodyAsString("https://api.weixin.qq.com/cgi-bin/token", resultData);
        } catch (Exception e) {
            e.printStackTrace();
        }
        logger.info("returnStr:[" + returnStr + "]");
        AccessToken accessToken = JSON.parseObject(returnStr, AccessToken.class);
        return accessToken;
    }
}
